package lzzy.edu.cn.demothymeleaf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lzzy.edu.cn.demothymeleaf.entity.Customer;
import lzzy.edu.cn.demothymeleaf.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class  UserService implements UserDetailsService {
    @Autowired
    private CustomerRepository customerRepository;
    //    @Cacheable(cacheNames = "customer",key = "result.id")
//    public Customer getCustomer(String username){
//        return customerRepository.findByUsername(username);
//    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        return customerRepository.findByUsername(username);
        Customer customer = customerRepository.findCustomerByUsername(username);
        if (customer!= null)
            return customer;
        else
            throw new UsernameNotFoundException("用户不存在");
    }

    //个人资料的修改
    public Customer findCustomerByUsername(String username) {
        return customerRepository.findCustomerByUsername(username);
    }
    public Customer findCustomerById(Integer id) {
        return customerRepository.findById(id).get();
    }
    public Customer saveCustomer(Customer customer) {
        return customerRepository.save(customer);
    }


    @Autowired
    private PasswordEncoder passwordEncoder;

    public Customer register(Customer customer) {
        customer.setPassword(passwordEncoder.encode(customer.getPassword()));
        return customerRepository.save(customer);
    }

}
